<template>
	<view>
		<uni-forms ref="form" :rules="{}">
			<uni-forms-item label="" name="">
				<uni-easyinput v-model="user.username" placeholder="请输入用户名" />
			</uni-forms-item>
			<uni-forms-item label="" name="">
				<uni-easyinput type="password" v-model="user.password" placeholder="请输入密码" />
			</uni-forms-item>
			<uni-forms-item label="" name="">
				<button type="primary" @click="login" class="button">登录</button>
				<button type="warn" @click="goregister" class="button">去注册</button>
			</uni-forms-item>
		</uni-forms>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				user: {
					username: 'tom',
					password: '123'
				}

			}
		},
		methods: {
			login(e) {
				uni.request({
					url: this.$baseUrl + 'user/login',
					method: 'POST',
					data: this.user,
					success: res => {
						if (res.data.success) {
							console.log(res);
							uni.setStorageSync('userInfo', res.data.data)
							uni.showToast({
								title: '' + res.data.data.username,
								icon: 'none'
							});
							uni.switchTab({
								url: '/pages/my/my'
							})
						} else {
							uni.setStorageSync('userInfo')
							uni.showToast({
								title: '失败',
								icon: 'none'
							});
						}


					},
					fail: (err) => {
						console.log(err);
					},
					complete: () => {}
				});
			},
			goregister() {
				uni.navigateTo({
				url:"/pages/my/register"
				})
			},
			onLoad: () => {
				uni.onTabBarMidButtonTap(function(e) {
					console.log(e)

				})
			}
		}

	}
</script>

<style>
	.button {
		margin: 10px;
	}
</style>